//
//  SINABasePickView.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/22.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "SINABasePickView.h"

//#define ScreenWidth [UIScreen mainScreen].bounds.size.width
//#define ScreenHeight [UIScreen mainScreen].bounds.size.height

@implementation SINABasePickView

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
- (instancetype)init
{
    self = [super init];
    if (self) {
        
        _pickerViewHeight      = 300;
        self.bounds = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.64];
        self.layer.opacity = 0.0;
        
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMiss)];
        self.userInteractionEnabled = YES;
        [self addGestureRecognizer:tap];
        
        [self addSubview:self.contentView];
        [self.contentView addSubview:self.pickerView];
        [self.contentView addSubview:self.cancelButton];
        [self.contentView addSubview:self.confirmButton];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.diviLabel];
        
        [self initPickView];
    }
    return self;
}
//初始化选择器内容，创建子类时需实现该父类方法
-(void)initPickView{
    
}
//点击确定按钮
- (void)clickConfirmButton
{
    [self disMiss];
}

//点击取消按钮
- (void) clickCancelButton
{
    [self disMiss];
}

//显示选择器
- (void)show
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self setCenter:[UIApplication sharedApplication].keyWindow.center];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    
    
    CGRect frame =  self.contentView.frame;
    frame.origin.y -= self.contentView.frame.size.height;
    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [self.layer setOpacity:1.0];
        self.contentView.frame = frame;
        
    } completion:^(BOOL finished) {
    }];
    
}

//移除选择器
- (void)disMiss
{
    
//    if (self.delegate) {
//        [self.delegate dismissBasePickView:self];
//    }
    
    CGRect frame =  self.contentView.frame;
    frame.origin.y += self.contentView.frame.size.height;
    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [self.layer setOpacity:0.0];
        self.contentView.frame = frame;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}
//设置选择器的高度
- (void)setPickerViewHeight:(CGFloat)pickerViewHeight
{
    _pickerViewHeight = pickerViewHeight;
    self.contentView.frame = CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.contentView.frame.size.width, pickerViewHeight);
}

- (UIView *)contentView
{
    if (!_contentView) {
        
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, self.pickerViewHeight)];
        [_contentView setBackgroundColor:[UIColor whiteColor]];
    }
    return _contentView;
}
- (UILabel *) titleLabel{

    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/2-50, 4, 100, 44)];
        _titleLabel.textAlignment=NSTextAlignmentCenter;
        [_titleLabel setBackgroundColor:[UIColor whiteColor]];
    }
    return _titleLabel;
}

- (UILabel *) diviLabel{
    
    if (!_diviLabel) {
        _diviLabel = [[UILabel alloc]initWithFrame:CGRectMake(11, self.contentView.frame.size.height/2-12, 5, 51)];
        _diviLabel.textAlignment=NSTextAlignmentCenter;
        [_diviLabel setBackgroundColor:[UIColor colorWithHexString:@"#00c6ae"]];
        [_diviLabel setBackgroundColor:baseSelectColor];
    }
    return _diviLabel;
}

- (UIPickerView *)pickerView
{
    if (!_pickerView) {
        
        _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(10,  54, self.contentView.frame.size.width-20, self.contentView.frame.size.height-80)];
        //设置lable的圆角显示
        _pickerView.layer.cornerRadius =5;
        //设置lable的边框 宽度为1
        _pickerView.layer.borderWidth=1;
        //设置lable的边框颜色
        _pickerView.layer.borderColor=[UIColor colorWithHexString:@"#eeeeee"].CGColor;
        //设置 显示大小范围为 border内容范围中
        _pickerView.clipsToBounds = YES;
        [_pickerView setBackgroundColor: [UIColor colorWithRed:0/255 green:198.0/255 blue:174.0/255 alpha:0.8f]];
        [_pickerView setBackgroundColor:[UIColor whiteColor]];
        
    }
    return _pickerView;
}

- (UIButton *)cancelButton
{
    if (!_cancelButton) {
        
        _cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(16, 4, 44, 44)];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:[UIColor colorWithHexString:@"#00c6ae"] forState:UIControlStateNormal];
        [_cancelButton setTitleColor:baseSelectColor forState:UIControlStateNormal];
        [_cancelButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_cancelButton addTarget:self action:@selector(clickCancelButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}

- (UIButton *)confirmButton
{
    if (!_confirmButton) {
        
        _confirmButton = [[UIButton alloc]initWithFrame:CGRectMake(self.contentView.frame.size.width - self.cancelButton.frame.size.width - self.cancelButton.frame.origin.x, self.cancelButton.frame.origin.y, self.cancelButton.frame.size.width, self.cancelButton.frame.size.height)];
        [_confirmButton setTitle:@"确定" forState:UIControlStateNormal];
        [_confirmButton setTitleColor:baseSelectColor forState:UIControlStateNormal];
        [_confirmButton setTitleColor:baseSelectColor forState:UIControlStateNormal];
        
        [_confirmButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_confirmButton addTarget:self action:@selector(clickConfirmButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmButton;
}

@end
